package com.woniuxy.factory.controller;

import com.woniuxy.factory.entity.Report;
import com.woniuxy.factory.entity.ResponseResult;
import com.woniuxy.factory.exception.BusinessException;
import com.woniuxy.factory.service.ReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/report")
public class ReportController {
    @Autowired(required = false)
    ReportService rs;

    //生成之间先进行判断当前计划单是否已生成报工单
    @RequestMapping("/selectreportbyplanid")
    @ResponseBody
    public ResponseResult selectReportByPlanid(String planid){
        ResponseResult rr;
        try{
            rs.selectReportByPlanid(planid);
            rr = new ResponseResult(0,"",0,null);
        }catch (BusinessException e){
            rr = new ResponseResult(1,e.getMessage(),0,null);
        }catch (Exception e){
            rr = new ResponseResult(2,"查询失败",0,null);
        }
        return rr;
    }

    //生产报工单
    @ResponseBody
    @RequestMapping("/addreport")
    public ResponseResult addReport(Report report){
        ResponseResult rr;
        try{
            rs.insertReport(report);
            rr = new ResponseResult(0,"生产报工单成功",0,null);
        }catch (Exception e){
            rr = new ResponseResult(1,"生成报工单失败",0,null);
        }
        return rr;
    }

    //查询所有报工单
    @ResponseBody
    @RequestMapping("/allreport")
    public ResponseResult selectAllReport(Report report,int page,int limit){
        List<Report> lr =  rs.selectAllReport(report,page,limit);
        int count = rs.selectReportCounts(report);
        ResponseResult rr = new ResponseResult(0,"",count,lr);
        return rr;
    }

    //删除报工单
    @RequestMapping("/deletereport")
    @ResponseBody
    public ResponseResult deleteReportById(String repId){
        ResponseResult rr;
        try{
            rs.deleteReportById(repId);
            rr = new ResponseResult(0,"删除成功",0,null);
        }catch (Exception e){
            rr = new ResponseResult(1,"删除失败",0,null);
        }
        return rr;
    }

    //修改数据回显
    @ResponseBody
    @RequestMapping("/reportecho")
    public ResponseResult reportEcho(String repId){
        Report r = rs.reportEcho(repId);
        ResponseResult rr = new ResponseResult(0,"",0,r);
        return rr;
    }

    //修改
    @ResponseBody
    @RequestMapping("/updateReport")
    public ResponseResult updateReport(Report report){
        ResponseResult rr;
        try{
            rs.updateReportById(report);
            rr = new ResponseResult(0,"修改成功",0,null);
        }catch (Exception e){
            rr = new ResponseResult(1,"修改失败",0,null);
        }
        return rr;
    }
}
